import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';
import _ from 'lodash';
import {Backend} from '../../providers/backend';
import moment from 'moment';
import {ImagePipe} from '../../pipes/pipes';

import {LoginPage} from '../tab-my/tab-my-login';
import {Meetingsignup} from './tab-meeting-signup';
import {Group} from '../tab-resources/tab-resources-group';
@Component({
  templateUrl: 'build/pages/tab-events/tab-upcomingInfo.html',
  pipes: [ImagePipe],
})

export class TabUpcomingInfo {
  
  static get parameters() {
    return [[NavController], [NavParams], [Backend]];
  }

  constructor(nav, params, backend) {
    this.nav = nav;
    this.params = params;
    this.backend = backend;
    this.result={};
    this.mon;
    this.num=0;
    this.fuwu='http://www.gtigroup.org:17001/app-portal/app/download?f=';
    this.confirmed=[];
    this.item = params.get('item');
    this.id = this.item .id;
    this.kind = this.item.kind;
    this.day;
    this.confirmedSpeakers;
    // let map = new GoogleMap('summitMap');
    // let element: HTMLElement = document.getElementById('summitMap');
    // let map = new GoogleMap(mapElement);
    // map.one(GoogleMapsEvent.MAP_READY).then(() => console.log('Map is ready!'));
    // let ionic: GoogleMapsLatLng = new GoogleMapsLatLng(43.0741904, -89.3809802);
    // let position: CameraPosition = {
    //   target: ionic,
    //   zoom: 18,
    //   tilt: 30
    // };

    this.mySlideOptions = {
      initialSlide: 0,
      loop: false,
      pager: true
    };
    this.backend.dsFind({
      e: params.data.item.id + '-timelineEvent-edit'
    }).then((result) => {
      console.log( params.data.item.id + '-timelineEvent-edit');
      this.result = result;
      this.agen=this.result.agendas.length;
      this.high=this.result.highlights.length;
      this.length=result.confirmedSpeakers.length;
      this.num1=Math.ceil(this.length/4);
      this.confirmedSpeakers=result.confirmedSpeakers.slice(this.num, this.num+4);
     
      for(let i =0;i<this.result.confirmedSpeakers.length;i++){
this.result.confirmedSpeakers[i].sort=parseInt(this.result.confirmedSpeakers[i].sort);
      }
       this.result.confirmedSpeakers=_.sortBy(this.result.confirmedSpeakers, ['sort']);
      console.log(this.result.confirmedSpeakers);
      for(let i =0;i<this.num1;i++){     
        if((this.num+4)>=this.length){
          console.log(this.num);
          this.confirmed[i]=result.confirmedSpeakers.slice(this.num, 4*(i+1));
          console.log(this.confirmed);
        }else{this.confirmed[i]=result.confirmedSpeakers.slice(this.num, 4*(i+1));
          console.log(this.confirmed);
          this.num=this.num+4;
        }     
      }
      this.result.agendas=_.sortBy(this.result.agendas, ['sort']);
      this.result.highlights=_.sortBy(this.result.highlights, ['sort']);
      this.day=moment().to(moment(result.timeBegin, 'YYYY-MM-DD'),true);
      this.day=Math.ceil((moment(result.timeBegin, 'YYYY-MM-DD').unix()-moment().unix())/60/60/24);
      // this.day = moment(result.timeBegin, 'YYYY-MM-DD').to();
      this.mon = moment(result.timeBegin, 'YYYY-MM-DD').format('Do MMMM  YYYY');
      // this.meetingfile=this.result.meetingFile.id.substring(this.result.meetingFile.id.length-36,this.result.meetingFile.id.length);
      console.log(this.meetingfile);
      console.log(this.result);
      // loading.dismiss();
    });
    
  }
 
  ionViewDidEnter() {
    this.map = new AMap.Map('summitMap', {
      lang: 'en',
      zoom: 18,
      resizeEnable: true,
      center: this.result.geoLocation.split(',')
    });
    this.position = this.result.geoLocation.split(',');
    marker = new AMap.Marker({
      position: this.position,
      map: this.map
    });
  }

  signup(mid){
    this.backend.isLogined()
      .then(result => {
        this.result1=result;
        console.log(this.result1);
        if(this.result1.didTutorial == 'true'){
          this.nav.rootNav.push(Meetingsignup,{'email':this.result.email,'uid':this.result.uid,'id':mid});
        }else{
          this.nav.rootNav.push(LoginPage);
          
        }
      });
    
  }
  file(meetingfile){
     this.nav.rootNav.push(Group,{'thid':meetingfile});
  }
  locReturn() {
    if (!this.result) {
      return;
    }
    this.map.setCenter(this.result.geoLocation.split(','));
  }
  open(link){
    cordova.InAppBrowser.open(link, '_system', 'location=no'); 
  }
}